package cn.demengsite.controller;

import cn.demengsite.dao.MemberDAO;
import cn.demengsite.model.BaseInfo;
import cn.demengsite.model.Member;
import cn.demengsite.service.MemberService;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
import java.util.stream.Collectors;

@Controller
public class MemberController  {

    private   final Logger logger = Logger.getLogger(MemberController.class.getName());

    @Autowired
    private MemberDAO memberDAO;

    @Autowired
    private MemberService memberService;



    @GetMapping("/member/showMemberPage")
    public String showMemberPage(){
        logger.info("come in member");
        return "member";
    }

    @GetMapping("/member/getAllMember")
    @ResponseBody
    public String getAllMember(){
        logger.info("come in getAllMember");
        List<Member> memberList = memberDAO.getAllMember();
        HashMap<String ,List> map = new HashMap<>();

        return JSON.toJSONString(memberList);
    }

    @GetMapping("/member/getMemberBirth")
    @ResponseBody
    public String getMemberBirth(){
        logger.info("come in getMemberBirth");
        List<Member> memberList = memberDAO.getAllMember();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String today  = format.format(new Date());
        String month = today.split("-")[1];
        List<Member> avaMemberList = new ArrayList<>();
        for (Member member : memberList) {
            if(member.getChildrenBirthday()!=null && member.getChildrenBirthday().split("-")[1].equals(month))
                avaMemberList.add(member);
        }
        HashMap<String ,List> map = new HashMap<>();
        map.put("memberList",avaMemberList);

        return JSON.toJSONString(avaMemberList);
    }

    @GetMapping("/member/getMember/{word}")
    @ResponseBody
    public String getMemberByCardOrMobile(@PathVariable String word){
        logger.info("come in getMemberByCardOrMobile");
        BaseInfo baseInfo = memberService.getMemberByCardOrMobile(word);
        return JSON.toJSONString(baseInfo);
    }

    @GetMapping("/member/recharge")
    @ResponseBody
    public String recharge(@RequestParam Double rechargeValue,@RequestParam Integer id){
        logger.info("come in recharge");
        BaseInfo baseInfo = memberService.memberRecharge(rechargeValue,id);
        return JSON.toJSONString(baseInfo);
    }

    @PostMapping("/member/newMember")
    @ResponseBody
    public String newMember(Member member){
        logger.info("come in newMember");
        BaseInfo baseInfo = memberService.addNewMember(member);
        return JSON.toJSONString(baseInfo);
    }
    @PostMapping("/member/updateMember")
    @ResponseBody
    public String updateMember(Member member){
        logger.info("come in updateMember");
        BaseInfo baseInfo = memberService.updateMember(member);
        return JSON.toJSONString(baseInfo);
    }

    @GetMapping("/member/getMemberSale/{id}")
    @ResponseBody
    public String getMemberSale(@PathVariable Integer id){
        logger.info("come in getMemberByCardOrMobile");
        BaseInfo baseInfo = memberService.getMemberSale(id);
        return JSON.toJSONString(baseInfo);
    }


}
